package com.liss.lissaiagent.tool;


import cn.hutool.core.io.FileUtil;
import com.liss.lissaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * @Author: Mr.Li
 * @CreateTime: 2025-10-15
 * @Description: 工具调用-文件读写
 * @Version: 1.0
 */
public class FileOpertionTool {

    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "Name of the file to read") String fileName) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
//            FileReader fileReader = new FileReader(filePath);
//            return fileReader.readString();

            String result = FileUtil.readUtf8String(filePath);
            return result;
        } catch (Exception e) {
            return "error reading file " + fileName;
        }
    }


    @Tool(description = "Write content to a file")
    public String wirteFile(@ToolParam(description = "Name of the file to write") String fileName,
                            @ToolParam(description = "Content to write to the file") String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(filePath, content);
            return "File written to " + fileName;
        }catch (Exception e){
            return "error writing file " + fileName;
        }
    }



}
